package com.mit.service.core.dao.impl;

import java.util.HashMap;

import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.core.dao.ISequenceDAO;

public class SequenceDAOImpl extends AbstractDAO implements ISequenceDAO {

	private final String KEY_NAME = "name";
	private final String KEY_YEAR = "year";
	private final String KEY_MONTH = "month";
	private final String KEY_SEASON = "season";
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public int getSequence(String category, Integer year, Integer month, Integer season){
		HashMap param = new HashMap();
		param.put(KEY_NAME, category);
		param.put(KEY_YEAR, year);
		param.put(KEY_SEASON, season);
		param.put(KEY_MONTH, month);
		
		//从数据库中取当前seq
		Integer seq = (Integer) getSqlMapClientTemplate().queryForObject("retrieveSeq", param);
		//如果没有，就增加一条记录到seq中
		if(seq == null){
			seq = 1;
			getSqlMapClientTemplate().insert("insertSeq", param);
		}

		//Update seq+1
		getSqlMapClientTemplate().update("updateSeq", param);
		return seq;
	}

}
